package com.iwhalecloud.bss.kite.cucc.service.attr.handler;

import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;
import com.ztesoft.bss.common.exception.BssException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Objects;

/**
 * <Description> <br>
 * 端口带宽
 *
 * @author cheng.leizhen01<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2021年3月16日 <br>
 * @see com.iwhalecloud.bss.kite.cucc.service.attr.handler <br>
 * @since R9.0<br>
 */

public class LocalAccessCircuitBandwidthAttrHandler extends AbstractAttrHandler<KiteInst, AttrData> {

    @Autowired
    IQuerySceneInstService querySceneInstService;

    @Override
    public void before(KiteAttr attr, KiteInst inst, String value, AttrData data) {
        super.before(attr, inst, value, data);
        if (data.getTriggerStrategy() != null) {
            // 初始化加载页面时不校验，跳过初次加载时的属性 set 触发
            return;
        }
        if (KiteStringUtils.isEmpty(value)) {
            return;
        }
        String serviceOfferId = data.getServiceOfferId();
        if ("2826".equals(serviceOfferId)) {
            // 产品变更 校验升速、降速变更
            List<String> serviceOfferIdList = querySceneInstService.getExtParameterValue(data.getSceneInstId(), "BUSI_CHANGE_SERVICEOFFERID");
            String originValueName = getValueName(attr, attr.getOriginValue());
            String currentValueName = getValueName(attr, value);
            if (StringUtils.isBlank(originValueName) || StringUtils.isBlank(currentValueName) || Objects.isNull(serviceOfferIdList)) {
                return;
            }
            int originUnit = 0;
            int currentUnit = 0;
            if (originValueName.contains("K")) {
                originUnit = originValueName.endsWith("KBPS")||originValueName.endsWith("K") ? 1 : 1024;
                currentUnit = getcurrentUnit(currentValueName);
            } else if (originValueName.contains("M")) {
                originUnit = originValueName.endsWith("MBPS")||originValueName.endsWith("M") ? 1024 : 1;
                currentUnit = getcurrentUnit(currentValueName);
            } else if (originValueName.contains("G")) {
                originUnit = originValueName.endsWith("GBPS")||originValueName.endsWith("G") ? 1024*1024 : 1;
                currentUnit = getcurrentUnit(currentValueName);
            }
            //割接数据名称不是数字+单位时不校验
            String originStr = originValueName.replaceAll("[^0-9\\.]", "");
            String currentStr = currentValueName.replaceAll("[^0-9\\.]", "");
            if(KiteStringUtils.isAnyEmpty(originStr,currentStr)){
                return;
            }
            double current = Double.parseDouble(currentStr) * currentUnit;
            double origin = Double.parseDouble(originStr) * originUnit;
            if (serviceOfferIdList.contains("1020")) {
                // 升速
                if (origin > current) {
                    BssException.throwOut("", "", "升速带宽新值不能小于原始带宽值");
                }
            } else if (serviceOfferIdList.contains("1021")) {
                // 降速
                if (origin < current) {
                    BssException.throwOut("", "", "降速带宽新值不能大于原始带宽值");
                }
            }
        }
    }

    private static String getValueName(KiteAttr attr, String OriginValue) {
        for (KiteAttrValue kiteAttrValue : attr.getAttrValues()) {
            if (kiteAttrValue.getAttrValue().equalsIgnoreCase(OriginValue)) {
                return kiteAttrValue.getAttrValueName();
            }
        }
        return null;
    }

    private int getcurrentUnit(String currentValueName) {
        int currentUnit = 0;
        if (currentValueName.contains("K")) {
            currentUnit = currentValueName.endsWith("KBPS")||currentValueName.endsWith("K") ? 1 : 1024;
        }else if (currentValueName.contains("M")) {
            currentUnit = (currentValueName.endsWith("MBPS")||currentValueName.endsWith("M") )? 1024 :1;
        }else if(currentValueName.contains("G")){
            currentUnit = currentValueName.endsWith("GBPS")||currentValueName.endsWith("G") ? 1024*1024 :1;
        }
        return currentUnit;
    }

}
